home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / sysdeps / unix / bsd / poll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  2.2 KB  |  82 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <sys/poll.h>
  20. #include <sys/types.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <sys/time.h>
  24.  
  25. /* Poll the file descriptors described by the NFDS structures starting at
  26.    FDS.  If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
  27.    an event to occur; if TIMEOUT is -1, block until an event occurs.
  28.    Returns the number of file descriptors with events, zero if timed out,
  29.    or -1 for errors.  */
  30.  
  31. int
  32. poll (fds, nfds, timeout)
  33.      struct pollfd *fds;
  34.      unsigned long int nfds;
  35.      int timeout;
  36. {
  37.   struct timeval tv;
  38.   fd_set rset, wset, xset;
  39.   struct pollfd *f;
  40.   int ready;
  41.   int maxfd = 0;
  42.  
  43.   FD_ZERO (&rset);
  44.   FD_ZERO (&wset);
  45.   FD_ZERO (&xset);
  46.  
  47.   for (f = fds; f < &fds[nfds]; ++f)
  48.     if (f->fd >= 0)
  49.       {
  50.     if (f->events & POLLIN)
  51.       FD_SET (f->fd, &rset);
  52.     if (f->events & POLLOUT)
  53.       FD_SET (f->fd, &wset);
  54.     if (f->events & POLLPRI)
  55.       FD_SET (f->fd, &xset);
  56.     if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
  57.       maxfd = f->fd;
  58.       }
  59.  
  60.   tv.tv_sec = timeout / 1000;
  61.   tv.tv_usec = (timeout + 999) / 1000;
  62.  
  63.   ready = __select (maxfd + 1, &rset, &wset, &xset,
  64.             timeout == -1 ? NULL : &tv);
  65.   if (ready > 0)
  66.     for (f = fds; f < &fds[nfds]; ++f)
  67.       {
  68.     f->revents = 0;
  69.     if (f->fd >= 0)
  70.       {
  71.         if (FD_ISSET (f->fd, &rset))
  72.           f->revents |= POLLIN;
  73.         if (FD_ISSET (f->fd, &wset))
  74.           f->revents |= POLLOUT;
  75.         if (FD_ISSET (f->fd, &xset))
  76.           f->revents |= POLLPRI;
  77.       }
  78.       }
  79.  
  80.   return ready;
  81. }
  82.